﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.DAL;
using System.Text;
namespace BOMS.WebUI.Web.DataAnalyse
{
    public partial class voltageDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                    //设置排序
                this.GridView_detail.Attributes.Add("SortExpression", "batteryid");
                this.GridView_detail.Attributes.Add("SortDirection", "ASC");

                ViewState["SortOrder"] = "batteryid";
                ViewState["OrderDire"] = " ASC"; 
                dbind();
            }

        }
        public void dbind()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView_detail.Attributes["SortExpression"];
            string sortDirection = this.GridView_detail.Attributes["SortDirection"];
            // 调用业务数据获取方法
            string groupLogId = Request.QueryString["logid"].ToString();
            DataTable dt = SingleHistory.getBatOfGroupByLogId(groupLogId);

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }


            if (dt.Rows.Count > 0)
            {
                this.GridView_detail.DataSource = dt;
                this.GridView_detail.DataBind();

                this.title.Text = "现场主机:" + dt.Rows[0]["hostname"] + " 电池组:" + dt.Rows[0]["batterygroup"] + " 时间:" + ((DateTime)(dt.Rows[0]["logtime"])).ToString("yyyy-MM-dd HH:mm:ss") + " 历史数据";
                this.note.Visible = false;
            }
            else
            {
                this.note.Text = "没有符合条件的数据！";
            }
        }

        protected void GridView_detail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //排序图标
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ShowSortedIcon(GridView_detail, e.Row);
            }
            if (e.Row.RowType == DataControlRowType.DataRow)//判断是不是数据行
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#abcdef'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

            }
        }
        protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)
        {
            for (int index = 0; index <= gvData.Columns.Count - 1; index++)
            {
                if ((gvData.Columns[index].SortExpression == gvSortExpression) && (gvData.Columns[index].SortExpression != ""))
                {
                    Image img = new Image();
                    if ((string)ViewState["OrderDire"] == " ASC")
                    {
                        img.ImageUrl = "../../Images/asc.png";
                        img.ToolTip = "递增排列";
                    }
                    else
                    {
                        img.ImageUrl = "../../Images/desc.png";
                        img.ToolTip = "递减排列";
                    }
                    gvRow.Cells[index].Controls.Add(img);
                }
            }
        }
        private string gvSortExpression
        {
            get
            {
                if (ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() != string.Empty)
                { return ViewState["SortOrder"].ToString(); }
                else
                { return ""; }
            }
            set
            { ViewState["SortOrder"] = value; }
        }   
        protected void GridView_detail_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView_detail.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView_detail.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");

                if (ViewState["OrderDire"].ToString() == " Desc")
                {
                    ViewState["OrderDire"] = " ASC";
                    ViewState["SortOrder"] = sortExpression;

                }
                else
                {
                    ViewState["OrderDire"] = " Desc";
                    ViewState["SortOrder"] = sortExpression;
                }
            }

            // 重新设定GridView排序数据列及排序方向
            this.GridView_detail.Attributes["SortExpression"] = sortExpression;
            this.GridView_detail.Attributes["SortDirection"] = sortDirection;
            gvSortExpression = e.SortExpression;
            dbind();
        }
    }
}